# Maintainer: George Rawlinson
# Contributor: Pekka Ristola <pekkarr [at] protonmail [dot] com>
# Contributor: Caltlgin Stsodaat <contact@fossdaily.xyz>

pkgname=python-rapidfuzz
pkgver=3.4.0
pkgrel=1
pkgdesc='Rapid fuzzy string matching in Python using various string metrics'
arch=('loong64' 'x86_64')
url='https://github.com/maxbachmann/rapidfuzz'
license=('MIT')
depends=(
  'glibc'
  'gcc-libs'
  'python'
)
makedepends=(
  'git'
  'python-build'
  'python-installer'
  'cython'
  'python-scikit-build'
  'rapidfuzz-cpp'
)
checkdepends=('python-hypothesis' 'python-pandas' 'python-pytest')
optdepends=('python-numpy')
_commit='1ccb012e50e819f0dc3ed9b20185c61d8e4b308c'
source=(
  "$pkgname::git+$url#commit=$_commit"
  'github.com-taskflow-taskflow::git+https://github.com/taskflow/taskflow'
  'remove-version-constraints.patch'
)
b2sums=('SKIP'
        'SKIP'
        'db458ca7daab8c2004fe84384fe3aa1f1403f58e59ffb7108617ee5b9bfb4905726c23237a8e4303220a29d8b4edaa992fabafab8ae72f5067adce06bf0afed2')

pkgver() {
  cd "$pkgname"

  git describe --tags | sed 's/^v//'
}

prepare() {
  cd "$pkgname"

  # remove version constraints
  patch -p1 -i "$srcdir/remove-version-constraints.patch"

  # prepare git submodules
  git submodule init extern/taskflow
  git config submodule.extern/taskflow.url "$srcdir/github.com-taskflow-taskflow"
  git -c protocol.file.allow=always submodule update
}

build() {
  cd "$pkgname"

  RAPIDFUZZ_BUILD_EXTENSION=1 \
    python -m build --wheel --no-isolation
}

check() {
  cd "$pkgname"

  python -m venv --system-site-packages test-env
  test-env/bin/python -m installer dist/*.whl
  test-env/bin/python -m pytest
}

package() {
  cd "$pkgname"

  python -m installer --destdir="$pkgdir" dist/*.whl

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgname" README.md

  # symlink license file
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")
  install -d "$pkgdir/usr/share/licenses/$pkgname"
  ln -s "$site_packages/${pkgname#python-}-$pkgver.dist-info/LICENSE" \
    "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}
